Deploying serverless Django applications

by Steve JeffersonSept 8th, 2016

Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as "serverless" web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance - and at a fraction of the cost of your current deployments

Setting up a Virtual Environment

Zappa requires a Virtual Evironment to package the required python libraries into the S3 storage. to set up your virtual environment run the following commands.

mkdir venv
cd venv
virtualenv env
source env/bin/activate
pip install django zappa

Djagno Configuration

  1. Create your Django application, or navigate to an existing Django application on your system.
  2. Install the packages listed in the requirments.txt file into your virtual environment.
  3. Opend an elevated command prompt inside the project you want to deploy.

AWS configuration

  1. Register for an AWS account,AWS Accounts Include 12 Months of Free Tier Access.
  2. Download and setup the AWS cli.
  3. Create an IAM User with the required policies for the lambda function.
  4. Login to your AWS account through the cli.

Zappa Configuration

  1. Make sure your virtual environment is active and run
  2. Zappa init

    you should get this message:

              
      ███████╗ █████╗ ██████╗ ██████╗  █████╗
      ╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
        ███╔╝ ███████║██████╔╝██████╔╝███████║
       ███╔╝  ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
      ███████╗██║  ██║██║     ██║     ██║  ██║
      ╚══════╝╚═╝  ╚═╝╚═╝     ╚═╝     ╚═╝  ╚═╝
      Welcome to Zappa!
      Zappa is a system for running server-less Python web applications on AWS Lambda and AWS API Gateway.
      This `init` command will help you create and configure your new Zappa deployment.
      Let's get started!
                
  3. Zappa asks you to define which env you want to configure, as it’s able to handle multiple envs. stick with the default ‘dev’.

  4. Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
    What do you want to call this environment (default 'dev'):
    
  5. After that, it will read the AWS Credentials file and read all the available profiles. We should have only ‘default’ at the moment, so just press enter to confirm

    AWS Lambda and API Gateway are only available in certain regions. Let's check to make sure you have a profile set up in one that will work.
    We found the following profiles: default. Which would you like us to use? (default 'default'):
  6. Zappa needs an S3 bucket to push your app. You can use an existing bucket or let Zappa generate one for you. Type the name of the bucket you want to use or press Enter to let Zappa automatically generate a bucket.

    Your Zappa deployments will need to be uploaded to a private S3 bucket.
    If you don't have a bucket yet, we'll create one for you too.
    What do you want call your bucket? (default 'zappa-koav28q4d'):
  7. Zappa can recognize that our app is a Django project, quickening the init process. type in the location of the settings.py file(app_name.settings):

    It looks like this is a Django application!
    What is the module path to your projects's Django settings?
    (This will likely be something like 'your_project.settings')
    Where are your project's settings?:
  8. You can optionally deploy to all available regions in order to provide fast global service, Or you can specify the aws region you want to deploy the application into.Type n and specify ap-south-1 to deploy the application into the Mumbai server only.

    Would you like to deploy this application globally? (default 'n') [y/n/(p)rimary]:
  9. To deploy the application type:

    zappa deploy dev

    you should get the following output.

    Calling deploy for stage dev..
    Creating zappatest-dev-ZappaLambdaExecutionRole IAM Role..
    Creating zappa-permissions policy on zappatest-dev-ZappaLambdaExecutionRole IAM Role.
    Downloading and installing dependencies..
    Packaging project as zip..
    Uploading zappatest-dev-1496245095.zip (11.0MiB)..
    100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11.5M/11.5M [00:15<00:00, 751KB/s]
    Scheduling..
    Scheduled zappatest-dev-zappa-keep-warm-handler.keep_warm_callback!
    Uploading zappatest-dev-template-1496245132.json (1.6KiB)..
    100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.61K/1.61K [00:01<00:00, 1.23KB/s]
    Waiting for stack zappatest-dev to create (this can take a bit)..
    75%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                      | 3/4 [00:10<00:05,  5.48s/res]
    Deploying API Gateway..
    Deployment complete!: https://yoururl.ap-south-1.amazonaws.com/dev
  10. Setup the Database by running the following commands.

    zappa manage dev migrate
    zappa manage dev s3_sqlite_vacuum
    zappa update dev
  11. You need to add the url that is generated by zappa into ALLOWED_HOSTS which is located in the the settings.py file.

    ALLOWED_HOSTS = ['671unn74s3.execute-api.ap-south-1.amazonaws.com']

update the application by running the command zappa update dev. the application should be live on the url that is retuned.